package cn.com.eado.crm.v.weiget

import android.app.Activity
import android.content.Context
import android.content.res.TypedArray
import android.text.Editable
import android.text.InputType
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.LinearLayout
import cn.com.eado.crm.R
import kotlinx.android.synthetic.main.auto_spinner.view.*
import android.widget.ArrayAdapter
import cn.com.eado.crm.app.KEY_USER_ID
import cn.com.eado.crm.app.MyApp
import cn.com.eado.crm.app.extenstion.toast
import cn.com.eado.crm.app.utils.ListUtil
import cn.com.eado.crm.app.utils.SpUtil
import cn.com.eado.crm.m.db.MyDatabase
import kotlin.concurrent.thread


class AutoSpinner (val ctx: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : LinearLayout(ctx, attributeSet, defStyleAttr){
    constructor(ctx: Context, attributeSet: AttributeSet?) : this(ctx, attributeSet, 0)
    constructor(ctx: Context) : this(ctx, null)

    private var stringList: MutableList<String>? = null
    private var show = false


    companion object{
        private var province: String? = null
        private var cityList: MutableList<String>? = null
        private var hangYe: String? = null
        private var hangYeTypeList: MutableList<String>? = null
    }

    init {
        LayoutInflater.from(ctx).inflate(R.layout.auto_spinner, this)

        val typedArray: TypedArray = ctx.obtainStyledAttributes(attributeSet, R.styleable.AutoSpinner)
        val name = typedArray.getString(R.styleable.AutoSpinner_name)
        val inputClickable = typedArray.getBoolean(R.styleable.AutoSpinner_input_clickable, true)

        tvName.text = name
        setListInName(tvName.text.toString().trim())

        cb.setOnClickListener {
            if (!show){
                show = true
                //联动选择
                when{
                    tvName.text.toString().trim().contains("类型") -> {
                        if(hangYe?.isNotEmpty() == true) {
                            setList(hangYeTypeList!!)
                            hangYeTypeList?.clear()
                            hangYeTypeList = null
                            hangYe = null
                        }
                    }
                    tvName.text.toString().trim().contains("城市") -> {
                        if(province?.isNotEmpty() == true) {
                            setList(cityList!!)
                            cityList?.clear()
                            cityList = null
                            province = null
                        }
                    }
                }
                //
                showData()
            }else{
                show = false
                tvAutoComplete.dismissDropDown()
            }
            cb.isChecked = show
        }
        if(!inputClickable){
            tvAutoComplete.inputType = InputType.TYPE_NULL;//来禁止手机软键盘
//            tvAutoComplete.inputType = InputType.TYPE_CLASS_TEXT;//来开启软键盘
        }
        tvAutoComplete.setOnDismissListener {
            if (show){
                cb.isChecked = false
                if (tvAutoComplete.text.toString().isNotEmpty()){
                    show = false
                }
            }
            //联动选择，读出子list
            if (tvName.text.toString().trim().contains("省份")){
                province = tvAutoComplete.text.toString().trim()
                if(province?.isNotEmpty() == true){
                    thread {
                        val temp = MyDatabase.getInstance().provinceDao().queryCity(province?:"广东")
                        if(temp?.citys?.isNotEmpty() == true){
                            cityList = mutableListOf()
                            for(obj in temp.citys){
                                cityList?.add(obj.citysName)
                            }
                        }
                    }
                }
            }else if (tvName.text.toString().trim().contains("行业")){
                hangYe = tvAutoComplete.text.toString().trim()
                if(hangYe?.isNotEmpty() == true){
                    thread {
                        val temp = MyDatabase.getInstance().hangYeDao().queryWithName(hangYe?:"电力")
                        if(temp?.types?.isNotEmpty() == true){
                            hangYeTypeList = mutableListOf()
                            for(obj in temp.types){
                                hangYeTypeList?.add(obj.name)
                            }
                        }
                    }
                }
            }
        }
    }

    private fun setListInName(name: String) {
        thread {
            val list = mutableListOf<String>()
            when {
                name.contains("企业性质") -> {
                    val temp = MyDatabase.getInstance().xingZhiDao().queryAll()
                    if(temp?.isNotEmpty() == true){
                        for(obj in temp){
                            list.add(obj.name)
                        }
                    }
                }
                name.contains("行业") -> {
                    val temp = MyDatabase.getInstance().hangYeDao().queryAll()
                    if(temp?.isNotEmpty() == true){
                        for(obj in temp){
                            list.add(obj.name)
                        }
                    }
                }
                name.contains("省份") -> {
                    val temp = MyDatabase.getInstance().provinceDao().queryAll()
                    if(temp?.isNotEmpty() == true){
                        for(obj in temp){
                            list.add(obj.provinceName)
                        }
                    }
                }
//                name.contains("城市") -> {
//                }
                name.contains("归属集团") -> {
//                    val userId = SpUtil.readInt(MyApp.instance, KEY_USER_ID, 0)
//                    val temp = MyDatabase.getInstance().enterpriseInfoDao().queryWithUser2(userId)
//                    if(temp?.isNotEmpty() == true){
//                        for(obj in temp){
//                            list.add(obj.group)
//                        }
//                        //去重
//                        val list2 = ListUtil.removeDuplicate(list)
//                        list.clear()
//                        list.addAll(list2)
//                    }
                }
                name.contains("片区") -> {
                    val temp = MyDatabase.getInstance().areaDao().queryAll()
                    if(temp?.isNotEmpty() == true){
                        for(obj in temp){
                            list.add(obj.name)
                        }
                    }
                }
                name.contains("销售员") -> {
//                    val userId = SpUtil.readInt(MyApp.instance, KEY_USER_ID, 0)
//                    val temp = MyDatabase.getInstance().enterpriseInfoDao().queryWithUser2(userId)
//                    if(temp?.isNotEmpty() == true){
//                        for(obj in temp){
//                            list.add(obj.salesperson)
//                        }
//                        //去重
//                        val list2 = ListUtil.removeDuplicate(list)
//                        list.clear()
//                        list.addAll(list2)
//                    }
                }
                name.contains("企业") -> {
//                    val userId = SpUtil.readInt(MyApp.instance, KEY_USER_ID, 0)
//                    val temp = MyDatabase.getInstance().companyDao().queryWithUser(userId)
//                    if(temp?.isNotEmpty() == true){
//                        for(obj in temp){
//                            list.add(obj.department)
//                        }
//                        //去重
//                        val list2 = ListUtil.removeDuplicate(list)
//                        list.clear()
//                        list.addAll(list2)
//                    }
                }
                name.contains("设计院") -> {
//                    val userId = SpUtil.readInt(MyApp.instance, KEY_USER_ID, 0)
//                    val temp = MyDatabase.getInstance().enterpriseInfoDao().queryWithUser2(userId)
//                    if(temp?.isNotEmpty() == true){
//                        for(obj in temp){
//                            list.add(obj.department)
//                        }
//                        //去重
//                        val list2 = ListUtil.removeDuplicate(list)
//                        list.clear()
//                        list.addAll(list2)
//                    }
                }
                name.contains("类型") -> {
                }
                name.contains("出发地") -> {
                }
                name.contains("到达地") -> {
                }
                name.contains("拜访对象") -> {
                }
                name.contains("单位名称") -> {
//                    val userId = SpUtil.readInt(MyApp.instance, KEY_USER_ID, 0)
//                    val temp = MyDatabase.getInstance().enterpriseInfoDao().queryWithUser2(userId)
//                    if(temp?.isNotEmpty() == true){
//                        for(obj in temp){
//                            list.add(obj.department)
//                        }
//                        //去重
//                        val list2 = ListUtil.removeDuplicate(list)
//                        list.clear()
//                        list.addAll(list2)
//                    }
                }
                name.contains("客户分类") -> {
                    val temp = MyDatabase.getInstance().clientDao().queryAll()
                    if(temp?.isNotEmpty() == true){
                        for(obj in temp){
                            list.add(obj.name)
                        }
                    }
                }
                name.contains("跟踪方式") -> {
                    val temp = MyDatabase.getInstance().trackDao().queryAll()
                    if(temp?.isNotEmpty() == true){
                        for(obj in temp){
                            list.add(obj.name)
                        }
                    }
                }
                name.contains("竞争对手") -> {
                }
                name.contains("紧急程度") -> {
                    val temp = MyDatabase.getInstance().urgentDao().queryAll()
                    if(temp?.isNotEmpty() == true){
                        for(obj in temp){
                            list.add(obj.name)
                        }
                    }
                }
            }
            if(list.isNotEmpty()) setList(list)
        }
    }

    fun setList(list: List<String>){
        if (stringList == null) stringList = mutableListOf()
        stringList?.clear()
        stringList?.addAll(list)
    }

    private fun showData() {
        if(stringList == null) return
        val adapter = ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, stringList!!)
        tvAutoComplete.setAdapter(adapter)
        tvAutoComplete.showDropDown()
    }

    fun getText() : String {
        return tvAutoComplete.text.toString().trim()
    }

    fun setText(s: String){
        tvAutoComplete.text = Editable.Factory.getInstance().newEditable(s)
    }

    fun getName() : String {
        val s = tvName.text.toString().trim()
        return s.replace(":","").replace("：", "")
    }

    fun isEmpty(): Boolean{
        if (tvAutoComplete.text.toString().trim().isEmpty()){
            ctx as Activity
            ctx.toast("请选择${getName()}")
            return true
        }
        return false
    }
}